package com.zhx.threadcoreknowledge.stopthread.volatiledemo;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-10-01 10:07
 * 用途         ： 演示volatile的局限
 * part1 看似可行
 */
public class WrongWayVolatile implements Runnable {

    private volatile boolean canceled = false;

    @Override
    public void run() {
        int num = 0;

        try {
            while (num <= 1000000 && !canceled) {
                if (num % 100 == 0) {
                    System.out.println(num + "是100的倍数");
                }
                Thread.sleep(1);
                num ++;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        WrongWayVolatile wrongWayVolatile = new WrongWayVolatile();
        Thread thread = new Thread(wrongWayVolatile);
        thread.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        wrongWayVolatile.canceled = true;
    }
}
